<?php 
	include("includes/connect_db.php");

	$sql = "SELECT * FROM tb_respon WHERE status = 0";
	$query = mysql_query($sql);
	while ($result = mysql_fetch_array($query)) {
		$pengirim = $result['pengirim_pesan'];
		$creator = $result['creator_id'];
		$sqlUpdate = "UPDATE tb_respon set status = 1 WHERE id_respon = '$result[id_respon]'";
		$queryUpdate = mysql_query($sqlUpdate);

		$sqlOperasi = "SELECT * FROM tb_operasi WHERE id_operasi = '$result[id_operasi]'";
		$queryOperasi = mysql_query($sqlOperasi);
		$operasi = mysql_fetch_array($queryOperasi);

		$sqlHost = "SELECT * FROM tb_host WHERE id_host = '$operasi[id_host]'";
		$queryHost = mysql_query($sqlHost);
		$host = mysql_fetch_array($queryHost);

		$koneksiBaru = mysqli_connect($host['host'], $host['username'], $host['password']) or die("Connection Error: " . mysqli_error());
    	mysqli_select_db($koneksiBaru, $host['db']) or die("Error conecting to db.");


		switch ($operasi['id_jenis_sql']) {
			case '1':
				$sqlOutput = "SELECT * FROM tb_operasi_output WHERE id_operasi = '$result[id_operasi]'";
				$queryOutput = mysql_query($sqlOutput);
				$jumlahOutput = mysql_num_rows($queryOutput);
				$queryOutput = mysql_query($sqlOutput);

				$x = 1;
				while ($output = mysql_fetch_array($queryOutput)) {
					$arrOutput[$x][1] = $output['nama_output'];
					$arrOutput[$x][2] = $output['output'];
					$x++;
				}
				$queryRespon = $result['query_respon'];

			    if ($jumlahOutput = 1) {
			    	if ($respon = mysqli_query($koneksiBaru, $queryRespon)) {
			    		$hasil = mysqli_fetch_array($respon);
			    		$pesan =  $arrOutput[1][1]." = ".$hasil[0];
			    	} else {
			    		$pesan = "Maaf Data Yang Anda Masukkan Salah";
			    	}
			    } elseif ($jumlahOutput > 1) {
					$queryOutput = mysql_query($sqlOutput);
					$x = 1;
					$i = 0;
					while ($output = mysql_fetch_array($queryOutput)) {
						$pesan = $pesan.$arrOutput[$x][1]." = ".$hasil[$i];	
						if ($X < $jumlahOutput) {
							$pesan = $pesan." || ";
						}
						$x++;
						$i++;
					}							    	
			    }
				break;
			
			case '2':
				$queryRespon = $result['query_respon'];
				if ($respon = mysqli_query($koneksiBaru, $queryRespon)) {
					$pesan = "Data Anda Telah Berhasil Dimasukkan";
				} else {
					$pesan = "Maaf Data Yang Anda Masukkan Salah";
				}				

				break;

			case '3':
				$queryRespon = $result['query_respon'];
				if ($respon = mysqli_query($koneksiBaru, $queryRespon)) {
					$pesan = "Data Anda Telah Berhasil Di Update";
				} else {
					$pesan = "Maaf Data Yang Anda Masukkan Salah";
				}
				break;
			
			case '4':
				$queryRespon = $result['query_respon'];
				if ($respon = mysqli_query($koneksiBaru, $queryRespon)) {
					$pesan = "Data Anda Telah Berhasil Dihapus";
				} else {
					$pesan = "Maaf Data Yang Anda Masukkan Salah";
				}
				break;
			
		}
		$sql_kirim_pesan = "INSERT into outbox (TextDecoded, DestinationNumber, CreatorID) 
		    				VALUES ('$pesan', '$pengirim', '$creator')";
		$query_kirim_pesan = mysql_query($sql_kirim_pesan);
	}
?>